#include "mypushbutton.h"
#include <QPushButton>
#include <QDebug>
#include <QPropertyAnimation>

myPushButton::myPushButton(QString normalImg, QString pressImg )
{
    this->normalImgPath = normalImg;
    this->pressImgPath = pressImg;

    QPixmap pix;
    bool ret = pix.load(normalImg);
    if(!ret)
    {
        qDebug()<<normalImg<<"图片加载失败";
        return ;
    }

    //设置图片固定大小
    this->setFixedSize(pix.width(),pix.height());

    //设置图片不规则样式
    this->setStyleSheet("QPushButton{border:0px;}");

    //设置图标
    this->setIcon(pix);

    //设置图标大小
    this->setIconSize(QSize(pix.width(),pix.height()));
}

void myPushButton::zoom()
{
    //创建动态对象
    QPropertyAnimation *animation = new QPropertyAnimation(this,"geometry");

    //设置动画事件间隔
    animation->setDuration(200);

    //起始位置
    animation->setStartValue(QRect(this->x(),this->y(),this->width(),this->height()));

    //结束位置
    animation->setEndValue(QRect(this->x(),this->y()+10,this->width(),this->height()));

    //设置弹跳曲线
    animation->setEasingCurve(QEasingCurve::OutBounce);

    //起始位置
    animation->setStartValue(QRect(this->x(),this->y()-10,this->width(),this->height()));

    //结束位置
    animation->setEndValue(QRect(this->x(),this->y(),this->width(),this->height()));

    //设置弹跳曲线
    animation->setEasingCurve(QEasingCurve::OutBounce);

    //执行动画
    animation->start();
}
void myPushButton::mousePressEvent(QMouseEvent *e)
{
    //    传入按下图片不为空
    if(this->pressImgPath != "")
    {
        QPixmap pix;
        bool ret = pix.load(this->pressImgPath);
        if(!ret)
        {
            qDebug()<<this->pressImgPath<<"图片加载失败";
            return ;
        }

        //设置图片固定大小
        this->setFixedSize(pix.width(),pix.height());
        qDebug()<<pix.width()<<" "<<pix.height();
        //设置图片不规则样式
        this->setStyleSheet("QPushButton{border:0px;}");

        //设置图标
        this->setIcon(pix);

        //设置图标大小
        this->setIconSize(QSize(pix.width(),pix.height()));
    }

    //让父类执行其他内容
    return QPushButton::mousePressEvent(e);
}

void myPushButton::mouseReleaseEvent(QMouseEvent *e)
{
    //传入按下图片不为空
    if(this->pressImgPath != "")
    {
        QPixmap pix;
        bool ret = pix.load(this->normalImgPath);
        if(!ret)
        {
            qDebug()<<this->normalImgPath<<"图片加载失败";
            return ;
        }

        //设置图片固定大小
        this->setFixedSize(pix.width(),pix.height());

        //设置图片不规则样式
        this->setStyleSheet("QPushButton{border:0px;}");

        //设置图标
        this->setIcon(pix);

        //设置图标大小
        this->setIconSize(QSize(pix.width(),pix.height()));
    }

    //让父类执行其他内容
    return QPushButton::mouseReleaseEvent(e);
}
